/**
 * Title: jFamilyTree XSLT provider
 * Description: jFamilyTree provider using XSL-transformed GXL file
 * Version: provider
 * Copyright: (c) 2001-2008
 * Terms of use:see license agreement at http://treebolic.sourceforge.net/en/license.htm
 * Author: Bernard Bou
 * Company: bsys
 */
package jFamilyTree.provider.xslt;

import org.w3c.dom.Document;
import org.w3c.dom.DocumentType;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import bsys.xml.transformer.DomTransformer;

/**
 * Document dump
 * 
 * @author Bernard Bou
 */
public class DocumentDump
{
	/**
	 * Type names
	 */
	static private String[] theTypeNames = new String[Node.NOTATION_NODE + 1];

	static
	{
		theTypeNames[0] = "???";
		theTypeNames[Node.ELEMENT_NODE] = "element";
		theTypeNames[Node.ATTRIBUTE_NODE] = "node";
		theTypeNames[Node.TEXT_NODE] = "text";
		theTypeNames[Node.CDATA_SECTION_NODE] = "cdata";
		theTypeNames[Node.ENTITY_REFERENCE_NODE] = "entityref";
		theTypeNames[Node.ENTITY_NODE] = "entity";
		theTypeNames[Node.PROCESSING_INSTRUCTION_NODE] = "";
		theTypeNames[Node.COMMENT_NODE] = "comment";
		theTypeNames[Node.DOCUMENT_NODE] = "document";
		theTypeNames[Node.DOCUMENT_TYPE_NODE] = "doctype";
		theTypeNames[Node.DOCUMENT_FRAGMENT_NODE] = "fragment";
		theTypeNames[Node.NOTATION_NODE] = "notation";
	}

	/**
	 * Document to string
	 * 
	 * @param thisDocument
	 * @return string
	 */
	static public String toString(Document thisDocument)
	{
		StringBuffer thisBuffer = new StringBuffer();
		thisBuffer.append("vvvvv\n");
		try
		{
			String thisText = new DomTransformer().documentToString(thisDocument);
			thisBuffer.append(thisText);
		}
		catch (Exception thisException)
		{
		}
		thisBuffer.append("^^^^^");
		return thisBuffer.toString();
	}

	/**
	 * Document to tree of node types
	 * 
	 * @param thisDocument
	 *        document
	 * @return tree of node types
	 */
	static public String toStringType(Document thisDocument)
	{
		StringBuffer thisBuffer = new StringBuffer();
		thisBuffer.append("vvvvv\n");
		thisBuffer.append(toStringType(thisDocument, 0));
		thisBuffer.append("^^^^^");
		return thisBuffer.toString();
	}

	/**
	 * Node to tree of node types
	 * 
	 * @param thisNode
	 *        node
	 * @param thisLevel
	 *        level
	 * @return string for this node and children
	 */
	static private String toStringType(Node thisNode, int thisLevel)
	{
		StringBuffer thisBuffer = new StringBuffer();
		for (int i = 0; i < thisLevel; i++)
			thisBuffer.append(" ");

		thisBuffer.append(theTypeNames[thisNode.getNodeType()]);
		thisBuffer.append(" ");
		thisBuffer.append(thisNode.getNodeName());
		thisBuffer.append(" ");
		String thisValue = thisNode.getNodeValue();
		if (thisValue != null)
			thisValue = thisValue.replace('\n', '\u00B6');
		thisBuffer.append(thisValue);
		thisBuffer.append("\n");

		// recurse
		NodeList theseNodes = thisNode.getChildNodes();
		for (int i = 0; i < theseNodes.getLength(); i++)
			thisBuffer.append(toStringType(theseNodes.item(i), thisLevel + 1));

		return thisBuffer.toString();
	}

	/**
	 * DocType to string
	 * 
	 * @param thisDocTypeNode
	 *        DocType node
	 * @return string
	 */
	static public String toDocType(DocumentType thisDocTypeNode)
	{
		if (thisDocTypeNode != null)
		{
			StringBuffer thisBuffer = new StringBuffer();
			thisBuffer.append("Name=");
			thisBuffer.append(thisDocTypeNode.getName());
			thisBuffer.append("\n");
			thisBuffer.append("SystemId=");
			thisBuffer.append(thisDocTypeNode.getSystemId());
			thisBuffer.append("\n");
			thisBuffer.append("PublicId=");
			thisBuffer.append(thisDocTypeNode.getPublicId());
			thisBuffer.append("\n");
			NamedNodeMap thisEntityMap = thisDocTypeNode.getEntities();
			for (int i = 0; i < thisEntityMap.getLength(); i++)
			{
				Node thisItem = thisEntityMap.item(i);
				thisBuffer.append("entity ");
				thisBuffer.append(thisItem.getNodeName());
				thisBuffer.append("=");
				thisBuffer.append(thisItem.getNodeValue());
				thisBuffer.append("\n");
			}
			NamedNodeMap thisNotationMap = thisDocTypeNode.getNotations();
			for (int i = 0; i < thisNotationMap.getLength(); i++)
			{
				Node thisItem = thisNotationMap.item(i);
				thisBuffer.append("notation ");
				thisBuffer.append(thisItem.getNodeName());
				thisBuffer.append("=");
				thisBuffer.append(thisItem.getNodeValue());
				thisBuffer.append("\n");
			}
			return thisBuffer.toString();
		}
		return null;
	}
}
